<template>
    <el-tree v-if="config.type == 'tree'" :data="option" highlight-current node-key="pid" :show-checkbox="true" :filter-node-method="filterNode" @check-change="handleCheckChange" ref="tree">
        <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>
                <i :class="data.icon"></i>
                {{ node.label }}
            </span>
        </span>
    </el-tree>

    <el-cascader
        v-else
        :options="option"
        filterable
        :show-all-levels="false"
        clearable
        v-model="value"
        :props="{ multiple: multiple }"
        @change="
            (e) => {
                this.$emit('change', e)
            }
        "
    ></el-cascader>
</template>

<script>
import { mapState } from 'vuex'
import { requestApi } from '@/requset/api'
import { deepClone } from '@/plugins/clone'
export default {
    data() {
        return {
            option: [],
            userLevel: null,
        }
    },
    props: {
        value: {
            type: Array,
            default() {
                return []
            },
        },
        config: {
            type: Object,
            default() {
                return {}
            },
        },
        multiple: {
            type: Boolean,
            default() {
                return false
            },
        },
    },
    computed: {
        ...mapState('backstage', ['backstageData']),
    },
    methods: {},
    created() {
        this.userLevel = sessionStorage.getItem('userLevel')
        let options = deepClone(this.backstageData)
        requestApi('post', 'device/getlist', { device_name: null, page: 1, scope_id: null, scope_id_list: [], size: 999, type_id: null }).then((res) => {
            let digui = (arr) => {
                if (arr && arr.length != 0) {
                    arr.map((item) => {
                        if (item.type !== 'zone') digui(item.children)
                        else {
                            item.children = []
                            for (let i of res.data.data) {
                                if (i.zone_name == item.label)
                                    item.children.push({
                                        label: i.device_name,
                                        value: i.id,
                                    })
                            }
                        }
                    })
                }
            }
            digui(options)
            this.option = options
        })
    },
}
</script>

<style></style>
